package com.prj.ufdm.web.security.branch;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.prj.ufdm.web.enums.WebRetCodeEnum;
import com.prj.ufdm.core.dto.RetData;
import com.prj.ufdm.core.util.UfdmRequestUtil;

/**
 * 用户名、密码登录失败后处理
 * @author Administrator
 */
@Service
public class LoginFaiurelHandle  extends SimpleUrlAuthenticationFailureHandler{
	
	@Override
	public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,AuthenticationException exception) throws IOException, ServletException {
		UfdmRequestUtil.addCsrfHeader(response);
		RetData<String> retData = new RetData<>();
		retData.getRetHead().setCode(WebRetCodeEnum.RET_CODE_0801002.getCode());
		retData.getRetHead().setMessage(WebRetCodeEnum.RET_CODE_0801002.getMessage());
		response.getWriter().write(JSON.toJSONString(retData));
	}
}
